package ucm.module;

import air_cp5620.dao.DAO;
import javax.swing.JOptionPane;
import java.sql.*;


public class UC_LogM {

    private DAO dbCon = null;
    private String name = null;
    private String Password = null;
    private String roleid = null;

    public UC_LogM(String name, String Password,String roleId) {
        this.name = name;
        this.Password = Password;
        this.roleid = roleId;
        dbCon = new DAO();
    }



    public boolean runLogin() {
        String sql = null;
        Connection con = dbCon.getConnection();
        if (con == null) {
            return false;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "select * from tbl_user where userid = " + "'" + name + "'";
            ResultSet res = stmt.executeQuery(sql);
            
            res.first();
            if (Password.equals(res.getString("password")) && (roleid.equals(res.getString("roleid")))) {
                return true;
            }
            else {
                 JOptionPane.showMessageDialog(null, "Password or the role of error！");
                 return false;
            }
           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Does not have the user！");
            e.printStackTrace();
            return false;
        }
    }
}
